var e = getApp(), t = require("../../utils/config"), a = null, n = null;

Page({
    data: {
        cishu: 0,
        chaping: "",
        result01: !0,
        result02: !1,
        navData: [ {
            text: "盛夏光年"
        }, {
            text: "秘密花园"
        }, {
            text: "国风雅韵"
        }, {
            text: "白皙无暇"
        }, {
            text: "梦幻迷离"
        } ],
        currentTab: 0,
        navScrollLeft: 0,
        none: "none",
        display: "",
        title: "温柔浪漫夏日祭",
        name: "盛夏光年",
        img_filter: "img-filter",
        poster: "none",
        jili: ""
    },
    result01: function() {
        this.setData({
            result02: !0,
            result01: !1
        });
    },
    result02: function() {
        this.setData({
            result02: !1,
            result01: !0
        });
    },
    onLoad: function(e) {
        console.log("jili=" + wx.getStorageSync("wxapp").jili);
        var t = this, o = wx.getStorageSync("wxapp").chaping;
        o && (wx.createInterstitialAd && o && ((n = wx.createInterstitialAd({
            adUnitId: o
        })).onLoad(function() {}), n.onError(function(e) {}), n.onClose(function() {})), 
        n && n.show().catch(function(e) {
            console.error(e);
        })), wx.getSystemInfo({
            success: function(e) {
                t.setData({
                    pixelRatio: e.pixelRatio,
                    windowHeight: e.windowHeight,
                    windowWidth: e.windowWidth
                });
            }
        });
        var i = this;
        i.getVipPrice();
        var s = e.result_image;
        wx.setStorage({
            key: "result_image",
            data: s
        });
        var r = wx.getStorageSync("userImage"), c = wx.getStorageSync("key");
        if ("" == (u = wx.getStorageSync("cate"))) var u = 13;
        i.getNav(u), i.getList(u), i.setData({
            userImage: s,
            uploadImage: "https://image.faxingwu.com/" + r,
            key: c,
            display: "none"
        }), wx.createRewardedVideoAd && ((a = wx.createRewardedVideoAd({
            adUnitId: wx.getStorageSync("wxapp").jili
        })).onLoad(function() {}), a.onError(function(e) {}));
    },
    checkPay: function() {
        var t = this, a = wx.getStorageSync("user_id");
        console.log(a), "" != a && void 0 != a ? wx.getSystemInfo({
            success: function(a) {
                "ios" == a.platform ? t.setData({
                    display: "none",
                    none: "none",
                    img_filter: ""
                }) : e._get("user.index/detail", {}, function(e) {
                    0 == e.data.userInfo.vip ? t.setData({
                        display: "none",
                        img_filter: "",
                        none: "none"
                    }) : t.setData({
                        none: ""
                    });
                });
            }
        }) : (wx.showToast({
            title: "小程序系统错误",
            icon: "none",
            duration: 1e3
        }), wx.navigateTo({
            url: "/pages/index/index"
        }));
    },
    getNav: function(e) {
        13 == e && this.setData({
            currentTab: 0
        }), 11 == e && this.setData({
            currentTab: 1
        }), 7 == e && this.setData({
            currentTab: 2
        }), 5 == e && this.setData({
            currentTab: 3
        }), 3 == e && this.setData({
            currentTab: 4
        });
    },
    saveImage1: function() {
        wx.showToast({
            title: "该功能需要开通VIP，请购买VIP",
            icon: "none",
            duration: 2e3
        });
    },
    makePoster: function(e) {
        var a = this, n = e.currentTarget.dataset.image;
        wx.showLoading({
            title: "海报生成中..."
        }), wx.request({
            url: t.API_HOST + "/huihua/image/make-new-poster",
            data: {
                image: n
            },
            method: "POST",
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            success: function(e) {
                if (console.log(e.data), 101 == e.data.code) {
                    var t = e.data.poster;
                    a.setData({
                        posterImg: t,
                        poster: ""
                    }), a.savePoster(t), wx.hideLoading();
                } else wx.showToast({
                    title: "海报生成失败",
                    icon: "none",
                    duration: 1e3
                });
            }
        });
    },
    hiddenPoster: function() {
        this.setData({
            poster: "none"
        });
    },
    savePoster: function(e) {
        wx.getImageInfo({
            src: e,
            success: function(e) {
                var t = e.path;
                wx.saveImageToPhotosAlbum({
                    filePath: t,
                    success: function(e) {},
                    fail: function(e) {}
                });
            }
        });
    },
    getVipPrice: function() {
        var t = this;
        e._get("wxapp/base", {}, function(e) {
            var a = e.data.wxapp;
            wx.setStorageSync("wxapp", a);
            var n = a.day_price, o = a.month_price, i = a.year_price;
            t.setData({
                dayPrice: n,
                price: o,
                year: i
            });
        });
    },
    switchNav: function(e) {
        var t = e.currentTarget.dataset.current, a = this.data.windowWidth / 5;
        if (this.setData({
            navScrollLeft: (t - 3) * a
        }), this.data.currentTab == t) return !1;
        this.setData({
            currentTab: t,
            key: 1e4
        });
        var n = e.currentTarget.dataset.current;
        if (0 == n && (o = 13, this.setData({
            title: "温柔浪漫夏日祭",
            name: "盛夏光年"
        })), 1 == n && (o = 11, this.setData({
            title: "虚幻缥缈的世界",
            name: "秘密花园"
        })), 2 == n && (o = 7, this.setData({
            title: "水墨丹青中国风",
            name: "国风韵雅"
        })), 3 == n && (o = 5, this.setData({
            title: "清新唯美牛奶肌",
            name: "白皙无暇  "
        })), 4 == n) {
            var o = 3;
            this.setData({
                title: "镜花水月琉璃梦",
                name: "梦幻迷离"
            });
        }
        this.getList(o);
    },
    getList: function(e) {
        var a = this, n = wx.getStorageSync("uid");
        wx.request({
            url: t.API_HOST + "/huihua/index/get-image",
            data: {
                cate: e,
                uid: n
            },
            method: "POST",
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            success: function(e) {
                101 == e.data.code ? a.setData({
                    list: e.data.list
                }) : wx.showToast({
                    title: e.data.msg,
                    icon: "none",
                    duration: 1e3
                });
            }
        });
    },
    changeImage: function(e) {
        var t = this, a = e.currentTarget.dataset.type, n = wx.getStorageSync("uid"), o = wx.getStorageSync("image_id");
        if (1 == a) i = "none"; else var i = "";
        wx.chooseImage({
            count: 1,
            sizeType: [ "original", "compressed" ],
            sourceType: [ "album", "camera" ],
            success: function(e) {
                var a = e.tempFilePaths[0];
                wx.showLoading({
                    title: "图片上传中..."
                }), wx.uploadFile({
                    url: "https://imgapi.faxingwu.com/api/upload",
                    filePath: a,
                    name: "image",
                    formData: {
                        image: a,
                        type: 1,
                        proname: "manhua"
                    },
                    header: {
                        "Content-Type": "application/x-www-form-urlencoded"
                    },
                    success: function(e) {
                        var a = e.data, s = JSON.parse(a);
                        if (101 == s.code) {
                            wx.hideLoading();
                            var r = s.data[0];
                            wx.showLoading({
                                title: "图片检测中..."
                            }), wx.request({
                                url: "https://dou.j391.com/dongman/img_sec_check.php",
                                data: {
                                    p: "http://image.faxingwu.com" + r,
                                    appid: getApp().globalData.appid
                                },
                                method: "POST",
                                header: {
                                    "Content-Type": "application/x-www-form-urlencoded"
                                },
                                success: function(e) {
                                    console.log("e.errcode=" + e.data.errcode), wx.hideLoading(), 0 == e.data.errcode ? (wx.setStorageSync("userImage", r), 
                                    t.setData({
                                        display: "",
                                        none: i,
                                        uploadImage: "https://image.faxingwu.com/" + r
                                    }), t.makeInfo(o, n)) : wx.showToast({
                                        title: "内容含有违法违规内容",
                                        icon: "none",
                                        duration: 1e3
                                    });
                                }
                            });
                        } else wx.showToast({
                            title: "图片上传失败",
                            icon: "none",
                            duration: 1e3
                        });
                    }
                });
            }
        });
    },
    choose: function(e) {
        var t = this, a = e.currentTarget.dataset.id, o = e.currentTarget.dataset.key, i = wx.getStorageSync("user_id"), s = t.data.cishu;
        s++, console.log("cishu=" + s), s % 5 == 0 && n && n.show().catch(function(e) {
            console.error(e);
        }), t.setData({
            key: o,
            cishu: s
        }), wx.setStorage({
            key: "image_id",
            data: a
        }), wx.getSystemInfo({
            success: function(e) {
                t.makeInfo(a, i), t.setData({
                    display: "",
                    none: "none"
                });
            }
        });
    },
    dayVipPay: function() {
        var e = this, t = void 0;
        (t = wx.getStorageSync("wxapp")) ? e.buyVip(2, t.day_price) : wx.showToast({
            title: "支付错误，稍后重试",
            icon: "none",
            duration: 1e3
        });
    },
    vipPay: function() {
        var e = this, t = void 0;
        (t = wx.getStorageSync("wxapp")) ? e.buyVip(1, t.month_price) : wx.showToast({
            title: "支付错误，稍后重试",
            icon: "none",
            duration: 1e3
        });
    },
    push: function(e, a, n) {
        wx.requestSubscribeMessage({
            tmplIds: [ "hcuXy6j4SMiP_pE05rFcdY7fM3m4s0-AmQ6P5-DHME4" ],
            success: function(o) {
                wx.request({
                    url: t.API_HOST + "/huihua/push/push",
                    data: {
                        openid: e,
                        uid: a,
                        order_id: n
                    },
                    method: "GET",
                    success: function(e) {
                        console.log(e.data);
                    }
                }), console.log(o);
            },
            fail: function(e) {
                console.log(e), wx.showToast({
                    title: "你取消了订阅,将不再接受此小程序的任何订阅消息",
                    icon: "none",
                    duration: 2e3
                });
            }
        });
    },
    yearVipPay: function() {
        var e = this, t = void 0;
        (t = wx.getStorageSync("wxapp")) ? e.buyVip(12, t.year_price) : wx.showToast({
            title: "支付错误，稍后重试",
            icon: "none",
            duration: 1e3
        });
    },
    buyVip: function(t, a) {
        var n = this, o = function(t) {
            if (-10 === t.code) return e.showError(t.msg), !1;
            wx.requestPayment({
                timeStamp: t.data.payment.timeStamp,
                nonceStr: t.data.payment.nonceStr,
                package: "prepay_id=" + t.data.payment.prepay_id,
                signType: "MD5",
                paySign: t.data.payment.paySign,
                success: function(e) {
                    n.setData({
                        none: "none",
                        display: "none",
                        img_filter: ""
                    });
                },
                fail: function() {
                    e.showError("支付失败");
                }
            });
        };
        wx.showLoading({
            title: "正在处理..."
        }), e._post_form("vip/buyVip", {
            vip_type: t,
            total_price: a
        }, function(e) {
            console.log("success"), o(e);
        }, function(e) {
            console.log("fail");
        }, function() {
            console.log("complete");
        });
    },
    hiddenPay: function() {
        this.setData({
            none: "none"
        });
    },
    saveImage: function(e) {
        var t = this;
        // let wxapp = wx.getStorageSync("wxapp");
        // wxapp.jili.length > 2 ? wx.showModal({
        //     title: "温馨提示",
        //     content: "广告收入是我们维护好软件的基石，所以需要您观看广告，广告播放完以后点击右上角的关闭按钮即可正常使用，感谢您的谅解！",
        //     confirmText: "点击观看",
        //     confirmColor: "#FF0000",
        //     showCancel: !1,
        //     success: function(a) {
        //         t.saveImageshowModal(e);
        //     }
        // }) : 
        t.saveImageshowModal(e);
    },
    saveImageshowModal: function(e) {
        var t = this;
        wx.getSystemInfo({
            success: function(n) {
                console.log("n.platform=" + n.platform), a ? (a.show().catch(function() {
                    a.load().then(function() {
                        return a.show();
                    }).catch(function(a) {
                        t.saveImageDow(e), console.log("视频广告显示失败");
                    });
                }), a.onClose(function(a) {
                    a && a.isEnded ? t.saveImageDow(e) : wx.showToast({
                        title: "视频未播放完毕，不可保存",
                        icon: "none",
                        duration: 2e3
                    });
                })) : t.saveImageDow(e);
            }
        });
    },
    saveImageDow: function(e) {
        var t = this, a = e.currentTarget.dataset.image;
        return t.saveResult(a), !0;
    },
    saveResult: function(e) {
        wx.getImageInfo({
            src: e,
            success: function(e) {
                var t = e.path;
                wx.saveImageToPhotosAlbum({
                    filePath: t,
                    success: function(e) {
                        wx.hideLoading(), wx.showToast({
                            title: "图片保存成功",
                            icon: "success",
                            duration: 2e3
                        }), wx.navigateTo({
                            url: "/pages/share/share"
                        });
                    },
                    complete: function(e) {
                        wx.hideLoading();
                    }
                });
            }
        });
    },
    makeInfo: function(e, a) {
        var n = this;
        if ("" == a) var a = 0;
        var o = wx.getStorageSync("userImage");
        wx.request({
            url: t.API_HOST + "/huihua/result/new-create-image",
            data: {
                uid: a,
                image: o,
                image_id: e
            },
            method: "POST",
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            success: function(e) {
                if (101 == e.data.code) {
                    var t = e.data.result_img;
                    wx.setStorage({
                        key: "result_image",
                        data: t
                    }), wx.hideLoading(), n.setData({
                        userImage: t,
                        display: "none"
                    });
                } else wx.showToast({
                    title: "图片识别失败,请重新上传",
                    icon: "none",
                    duration: 1e3
                }), n.setData({
                    display: "none"
                });
            }
        });
    },
    switchTab: function(e) {
        var t = e.detail.current, a = this.data.windowWidth / 6;
        this.setData({
            currentTab: t,
            navScrollLeft: (t - 3) * a
        });
    },
    onShareAppMessage: function(e) {
        return {
            title: wx.getStorageSync("wxapp").navbar.makingtitle,
            path: "/pages/index/index",
            imageUrl: wx.getStorageSync("wxapp").navbar.imageUrl
        };
    }
});